package com.special.ResideMenuDemo;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import robot.HttpData;
import robot.HttpGetDataListener;
import robot.ListData;
import robot.TextAdapter;

import static com.special.ResideMenuDemo.R.*;

public class RobotActivity extends Activity implements HttpGetDataListener, OnClickListener {
    private HttpData httpData;
    private List<ListData> lists;
    private ListView lv;
    private EditText sendText;
    private Button sendBtn;
    private String content_str;
    private TextAdapter adapter;
    private String []welcome_array;
    private double currentTime, oldTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_robot);

        initView();
    }

    private void initView() {
        getActionBar().setTitle(string.robot);
        lv = (ListView) findViewById(id.lv);
        sendText = (EditText) findViewById(id.sendText);
        sendBtn = (Button) findViewById(id.send_btn);
        lists = new ArrayList<ListData>();
        sendBtn.setOnClickListener(this);
        adapter = new TextAdapter(lists, this);
        lv.setAdapter(adapter);
        ListData listData;
        listData = new ListData(getRandomWelcomeTips(), ListData.RECEIVER, getTime());
        lists.add(listData);
    }

    private String getRandomWelcomeTips() {
        String welcome_tip = null;
        welcome_array = this.getResources().getStringArray(R.array.welcome_tips);
        int index = (int) (Math.random()*(welcome_array.length-1));
        welcome_tip = welcome_array[index];
        return welcome_tip;
    }

    @Override
    public void getDataUrl(String data) {
//        System.out.println(data);
        parseText(data);
    }

    public void parseText(String str) {
        try {
            JSONObject jb = new JSONObject(str);
//            System.out.println(jb.getString("code"));
//            System.out.println(jb.getString("text"));
            ListData listData;
            listData = new ListData(jb.getString("text"), ListData.RECEIVER, getTime());
            lists.add(listData);
            adapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        getTime();
        content_str = sendText.getText().toString();
        sendText.setText("");
        String dropk = content_str.replace(" ", "");
        String droph = dropk.replace("\n", "");
        ListData listData;
        listData =new ListData(content_str, ListData.SEND, getTime());
        lists.add(listData);
        if (lists.size()>30) {
            for (int i = 0; i < lists.size(); i++) {
                lists.remove(i);
            }
        }
        adapter.notifyDataSetChanged();
        httpData = (HttpData) new HttpData("http://www.tuling123.com/openapi/api?key=d20467b5e6c340c3a18a4557ebf31693&info="+droph, this).execute();
    }

    private String getTime() {
        currentTime = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
        Date curDate = new Date();
        String str = format.format(curDate);
        if (currentTime-oldTime >= 5*60*1000) {
            oldTime = currentTime;
            return str;
        } else {
            return "";
        }
    }
}
